package com.ruoyi.business.service.impl;

import com.ruoyi.business.mapper.WsdRechargeRefundMapper;
import com.ruoyi.business.service.IWsdRechargeRefundService;
import com.ruoyi.common.core.domain.entity.WsdRechargeRefund;
import com.ruoyi.common.core.domain.vo.RechargeRefundVO;
import com.ruoyi.common.utils.date.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 充值退款配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-21
 */
@Service
public class WsdRechargeRefundServiceImpl implements IWsdRechargeRefundService
{
    @Autowired
    private WsdRechargeRefundMapper wsdRechargeRefundMapper;

    /**
     * 查询充值退款配置
     * 
     * @param configId 充值退款配置主键
     * @return 充值退款配置
     */
    @Override
    public WsdRechargeRefund selectWsdRechargeRefundByConfigId(Long configId)
    {
        return wsdRechargeRefundMapper.selectWsdRechargeRefundByConfigId(configId);
    }

    /**
     * 查询充值退款配置列表
     * 
     * @param wsdRechargeRefund 充值退款配置
     * @return 充值退款配置
     */
    @Override
    public List<WsdRechargeRefund> selectWsdRechargeRefundList(WsdRechargeRefund wsdRechargeRefund)
    {
        return wsdRechargeRefundMapper.selectWsdRechargeRefundList(wsdRechargeRefund);
    }

    /**
     * 新增充值退款配置
     * 
     * @param wsdRechargeRefund 充值退款配置
     * @return 结果
     */
    @Override
    public int insertWsdRechargeRefund(WsdRechargeRefund wsdRechargeRefund)
    {
        return wsdRechargeRefundMapper.insertWsdRechargeRefund(wsdRechargeRefund);
    }

    /**
     * 修改充值退款配置
     * 
     * @param wsdRechargeRefund 充值退款配置
     * @return 结果
     */
    @Override
    public int updateWsdRechargeRefund(WsdRechargeRefund wsdRechargeRefund)
    {
        return wsdRechargeRefundMapper.updateWsdRechargeRefund(wsdRechargeRefund);
    }

    /**
     * 批量删除充值退款配置
     * 
     * @param configIds 需要删除的充值退款配置主键
     * @return 结果
     */
    @Override
    public int deleteWsdRechargeRefundByConfigIds(Long[] configIds)
    {
        return wsdRechargeRefundMapper.deleteWsdRechargeRefundByConfigIds(configIds);
    }

    /**
     * 删除充值退款配置信息
     * 
     * @param configId 充值退款配置主键
     * @return 结果
     */
    @Override
    public int deleteWsdRechargeRefundByConfigId(Long configId)
    {
        return wsdRechargeRefundMapper.deleteWsdRechargeRefundByConfigId(configId);
    }

    @Override
    public RechargeRefundVO existsActiveSemester(Long schoolId, String formattedDate) {
        // 查询是否存在符合条件的学期配置
        return wsdRechargeRefundMapper.existsActiveSemester(formattedDate,schoolId);
    }

    @Override
    public RechargeRefundVO existsActive(Long schoolId, String end, String start) {
        // 查询是否存在符合条件的学期配置
        return wsdRechargeRefundMapper.existsActive(end,start,schoolId);
    }

    /**
     * 计算退费金额
     *
     * @param breakfast
     * @param lunch
     * @param dinner
     * @param schoolId
     * @return
     */
    @Override
    public BigDecimal calculateTotalRefund(Boolean breakfast, Boolean lunch, Boolean dinner, Long schoolId) {
        String currentDateString = DateTimeUtil.getCurrentDateString();
        RechargeRefundVO rechargeRefundVO = wsdRechargeRefundMapper.existsActiveSemester(currentDateString,schoolId);
        //查询出餐费
        BigDecimal totalRefund = BigDecimal.ZERO;
        if (breakfast) {
            totalRefund = totalRefund.add(rechargeRefundVO.getBreakfastFee());
        }
        if (lunch) {
            totalRefund = totalRefund.add(rechargeRefundVO.getLunchFee());
        }
        if (dinner) {
            totalRefund = totalRefund.add(rechargeRefundVO.getDinnerFee());
        }
        return totalRefund;
    }
}
